/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package persistence;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import model.Funcionario;


/**
 *
 * @author Cristian
 */
public class FuncionarioDAO {
    private static FuncionarioDAO instance = new FuncionarioDAO();
    private FuncionarioDAO(){
    }
    public static FuncionarioDAO getInstace(){
     return instance;
    }
    
    
    public void save(Funcionario funcionario) throws SQLException, ClassNotFoundException{
        Connection conn = null;
        Statement st = null;
        PacienteDAO pacienteDAO =  PacienteDAO.getInstace();
        pacienteDAO.save(funcionario);
        try {
              conn = DataBaseLocator.getInsLocator().getConnection();
              st = conn.createStatement();
              st.execute("insert into Funcionario (idPaciente, dtCadastro, salario, turno)" +
                        " values ('" + funcionario.getId() + "',  '" + funcionario.getDtCadastro() + "', '" + funcionario.getSalario() 
                      + "', '" + funcionario.getTurno() + "')");
              
            } catch(SQLException e) {
                throw e;
            } finally {
                closeResources(conn, st);
            }
        
    }
    
    public List<Funcionario> listarFuncionario(){
        Connection conn = null;
        Statement st = null;
        List<Funcionario> lista = new ArrayList<Funcionario>();
        try{
           conn = DataBaseLocator.getInsLocator().getConnection();
           st = conn.createStatement();
           st.executeQuery("Select * from funcionario");
           ResultSet rs = st.getResultSet();
           do{
              Funcionario f = new Funcionario();
               f.setId(rs.getInt("id"));
               f.setDtCadastro(rs.getDate("dtCadastro"));
               f.setDtNascimento(rs.getDate("dtNascimento"));
               f.setNome(rs.getString("nome"));
               f.setProfissao(rs.getString("profissao"));
               f.setSalario(rs.getDouble("salario"));
               f.setTurno(rs.getString("turno"));
               lista.add(f);
           }while(rs.next());
           return lista;
        }catch(Exception ex){
            ex.printStackTrace();
            return null;
        }finally{
            closeResources(conn, st);
        }
        

    }
    
        public Funcionario buscaFuncionarioPorNome(Integer id){
        Connection conn = null;
        Statement st = null;
        Funcionario f = new Funcionario();  
        try{          
           conn = DataBaseLocator.getInsLocator().getConnection();
           st = conn.createStatement();
           st.executeQuery("Select * from funcionario f where f.id=" + id + "");
           ResultSet rs = st.getResultSet();
           do{             
               f.setId(rs.getInt("id"));
               f.setDtCadastro(rs.getDate("dtCadastro"));
               f.setDtNascimento(rs.getDate("dtNascimento"));
               f.setNome(rs.getString("nome"));
               f.setProfissao(rs.getString("profissao"));
               f.setSalario(rs.getDouble("salario"));
               f.setTurno(rs.getString("turno"));
           }while(rs.next());
           return f;
            } catch (Exception e) {
               e.printStackTrace();
               return null;
            }finally{
                closeResources(null, null);
            }
            
        }

    public void closeResources(Connection conn, Statement st){
      try {
            if(st!=null) st.close();
            if(conn!=null) conn.close();
          } catch(SQLException e) {
                    
          }
    }
}
